#!/bin/bash

SVNSERVER=$1
PROJECT=$2
FOLDER=$3
VERSION=$4
TOMCAT=$5
TODAY=$(date +%Y%m%d)
CURRENTTIME="$(date +%Y%m%d%k%M%S)";
echo "$(date +%Y"-"%m"-"%d"_"%k":"%M":"%S)"-----------download $PROJECT-----------  
export JAVA_HOME=/usr/java/jdk1.7.0_79
export M2_HOME=/usr/local/maven
echo server: $SVNSERVER ,  project: $PROJECT  folder: $FOLDER , version: $VERSION , tomcat: $TOMCAT
INSTALLBASE=/tmp/package/bxn-install-extends
rm -rf $INSTALLBASE
echo mkdir $INSTALLBASE
mkdir $INSTALLBASE

echo target dir: $INSTALLBASE
if [ ! -d "$INSTALLBASE" ];then
	echo base dir is not exits, will exit.
	exit -1;
fi
echo mk bx_app directs
mkdir $INSTALLBASE/bx_app
mkdir $INSTALLBASE/bx_app/config
mkdir $INSTALLBASE/bx_app/data
mkdir $INSTALLBASE/bx_app/data/init
mkdir $INSTALLBASE/bx_app/program
mkdir $INSTALLBASE/bx_app/program/nginx
mkdir $INSTALLBASE/bx_app/program/nginx/nginx-1.7.0
mkdir $INSTALLBASE/bx_app/program/nginx/nginx-1.7.0/conf
mkdir $INSTALLBASE/bx_app/program/nginx/nginx-1.7.0/conf/module
mkdir $INSTALLBASE/bx_app/installer
mkdir $INSTALLBASE/bx_app/f_data

BASE=/tmp/package/$PROJECT


echo tmp base: $BASE , begin download project from svn
if [ -d $BASE ];then
	echo tmp project dir is not empty, rm -rf $BASE
	rm -rf $BASE
fi
export LC_CTYPE=en_US.UTF-8
if [ "null" = $FOLDER  ];then
	echo FOLDER is null, url is: svn export svn://$SVNSERVER/$PROJECT/$VERSION $BASE  
	svn -q export svn://$SVNSERVER/$PROJECT/$VERSION $BASE --username tiger --password svn555666
else
	echo url is: svn export svn://$SVNSERVER/$PROJECT/$FOLDER/$VERSION $BASE  
	svn -q export svn://$SVNSERVER/$PROJECT/$FOLDER/$VERSION $BASE --username tiger --password svn555666
fi
cd $BASE
sed -i '8c <version>'$VERSION'</version>' pom.xml
if [ -d "/usr/local/maven/repo/cn/boxiao" ];then
	echo rm -rf /usr/local/maven/repo/cn/boxiao
	rm -rf /usr/local/maven/repo/cn/boxiao
fi
JAVA_HOME=/usr/java/jdk1.7.0_79
echo begin to create war: mvn -q package -Dmaven.test.skip
mvn -q package -Dmaven.test.skip
if [ -d "/usr/local/maven/repo/cn/boxiao" ];then
	echo chmod -R 777 /usr/local/maven/repo/cn/boxiao
	chmod -R 777 /usr/local/maven/repo/cn/boxiao
fi
if [ ! -d "target" ]; then
	echo ERROR!!!!!!!'target folder not found ,exit.' 
	rm -rf $BASE
	exit -1;
fi
WAR=$(ls target | grep .war) 
echo war name is: $WAR
if [ ! -f "target/$WAR" ];then
	echo ERROR!!!!!!!'war file is not exist.' 
	rm -rf $BASE
	exit -1;
fi
# tomcat
TOMCATPATH=/var2/share/resource_library/11-公司产品安装包/bxn-tomcat7_x64
if [ ! -d "$TOMCATPATH/$TOMCAT" ];then
	echo ERROR!!!!$TOMCAT is not exits.
	exit -1;
else
	echo cp tomcat to tmp path
	cp -r $TOMCATPATH/$TOMCAT $INSTALLBASE/bx_app/program/
fi
CONTEXTPATH=${WAR%.war*}
WEBAPPS=$INSTALLBASE/bx_app/program/$TOMCAT/webapps
NGINXFILE=$CONTEXTPATH.conf
PROPERTIESFILE=$CONTEXTPATH.properties
echo $PROJECT: tomcat is $TOMCAT, nginx is $NGINXFILE, properties is $PROPERTIESFILE


BX_CONFIG=$INSTALLBASE/bx_app/config
BX_FDATA=$INSTALLBASE/bx_app/f_data
NGINXCONFPATH=$INSTALLBASE/bx_app/program/nginx/nginx-1.7.0/conf/module
BX_DATA=$INSTALLBASE/bx_app/data/init/$CONTEXTPATH
# data : all are in dirs
if [ ! -d "$BX_DATA" ];then
	mkdir $BX_DATA
fi
echo mv target/$WAR to $WEBAPPS
mv target/$WAR $WEBAPPS
if [ -d "script/install" ];then
	echo mv script/install/files to $BX_DATA
	mv script/install/* $BX_DATA
else
	echo $PROJECT: has no script to install.
fi
if [ -d "doc/f_data" ];then
	echo cp f_data to $BX_FDATA
	sh /usr/packagetools/mvfiles.sh doc/f_data $BX_FDATA
else
	echo $PROJECT: f_data is not exits.
fi
# nginx
if [ -f doc/$NGINXFILE ];then
	echo mv doc/$NGINXFILE $NGINXCONFPATH
	mv doc/$NGINXFILE $NGINXCONFPATH
else
	echo $PROJECT has no nginx file.
fi
cd $WEBAPPS
rm -rf $CONTEXTPATH
echo unzip $WAR -d $CONTEXTPATH
unzip -q $WAR -d $CONTEXTPATH
rm -rf $WAR 
echo remove $WAR finished
if [ $PROJECT = "bxn-static-resource" ] || [ $PROJECT = "cas-server" ];then
	echo $PROJECT has no classes
else
	echo begin jars: $PROJECT-$VERSION.jar
	cd $WEBAPPS/$CONTEXTPATH/WEB-INF/classes
	jar cvf  $PROJECT-$VERSION.jar *
	mv $PROJECT-$VERSION.jar ../lib/
	rm -rf ../classes
fi
# properties
if [ -d "$WEBAPPS/$CONTEXTPATH/WEB-INF" ];then
	echo cd $WEBAPPS/$CONTEXTPATH/WEB-INF
	cd $WEBAPPS/$CONTEXTPATH/WEB-INF
	if [ -f application/$PROPERTIESFILE ];then
		echo cp application/$PROPERTIESFILE $BX_CONFIG
		cp application/$PROPERTIESFILE $BX_CONFIG
	else	
		echo $PROJECT has no properties file.
	fi
else
	echo $PROJECT has no WEB-INF
fi
echo rm -rf $BASE
rm -rf $BASE
#  tomcat installer
INSTALLER=$INSTALLBASE/bx_app/installer;
INSTALLERBAT=$INSTALLER/install$TOMCAT.bat;
echo touch "$INSTALLERBAT";
touch "$INSTALLERBAT"
if [ -f "$INSTALLERBAT" ];then
	echo touch tomcatIntsallBat success!
else
	echo ERROR!! touch tomcatIntsallBat failed!
	exit -1;
fi
echo "{diskIndex}" >> $INSTALLERBAT
echo "cd {BX_HOME}\\program\\$TOMCAT\\bin" >> $INSTALLERBAT
echo "call service install $TOMCAT" >> $INSTALLERBAT
# install properties
INSTALLERPROPERTIES=$INSTALLER/bxn-install-extends.properties;
echo touch "$INSTALLERPROPERTIES";
touch "$INSTALLERPROPERTIES"
if [ -f "$INSTALLERPROPERTIES" ];then
	echo touch $INSTALLERPROPERTIES success!
else
	echo ERROR!! touch $INSTALLERPROPERTIES failed!
	exit -1;
fi

echo "moduleName=$CONTEXTPATH" >> $INSTALLERPROPERTIES;
echo "serviceToInstall=$TOMCAT" >> $INSTALLERPROPERTIES;
echo "serviceToStart=$TOMCAT" >> $INSTALLERPROPERTIES;
echo "param.romteAppPasswd={current_app_password}" >> $INSTALLERPROPERTIES;
echo "param.bxnPassword={bxnPassword}" >> $INSTALLERPROPERTIES;
echo "param.rootPassword={rootPassword}" >> $INSTALLERPROPERTIES;
echo "param.bx_home={BX_HOME}" >> $INSTALLERPROPERTIES;
echo "param.diskIndex={diskIndex}" >> $INSTALLERPROPERTIES;
echo "$TOMCAT=\\installer\\install$TOMCAT.bat" >> $INSTALLERPROPERTIES;
echo "romteAppPasswdConfigFile=$CONTEXTPATH" >> $INSTALLERPROPERTIES;
echo "romteAppPasswdConfigSQL=$CONTEXTPATH" >> $INSTALLERPROPERTIES;

# install java jar
INSTALLJAR=/var2/share/resource_library/11-公司产品安装包/bxn-install-jar;
echo cp $INSTALLJAR/bxn-install-extends.jar $INSTALLER/;
cp $INSTALLJAR/bxn-install-extends.jar $INSTALLER/;
if [ -f "$INSTALLER/bxn-install-extends.jar" ];then
	echo cp $INSTALLER/bxn-install-extends.jar success!;
else
	echo ERROR!! cp $INSTALLER/bxn-install-extends.jar failed!;
	exit -1;
fi
# data init bat
DATAINITUSER=$BX_DATA/init_mysql_user.sql;
echo touch "$DATAINITUSER";
touch "$DATAINITUSER";
if [ -f "$DATAINITUSER" ];then
	echo touch "$DATAINITUSER" success!
else
	echo ERROR!! touch "$DATAINITUSER" failed!
	exit -1;
fi
echo "GRANT all privileges ON $CONTEXTPATH.* TO 'bxn'@'127.0.0.1';flush privileges;" >> $DATAINITUSER;
DATAINITBAT=$BX_DATA/init.bat;
echo touch "$DATAINITBAT";
touch "$DATAINITBAT";
if [ -f "$DATAINITBAT" ];then
	echo touch "$DATAINITBAT" success!
else
	echo ERROR!! touch "$DATAINITBAT" failed!
	exit -1;
fi
echo "mysql -h127.0.0.1 -uroot -p{rootPassword} --default-character-set=utf8 < \"{BX_HOME}\\data\\init\\$CONTEXTPATH\\init_mysql_user.sql\"" >> $DATAINITBAT;
echo "mysql -h127.0.0.1 -ubxn -p{bxnPassword} --default-character-set=utf8 < \"{BX_HOME}\\data\\init\\$CONTEXTPATH\\init_table.sql\"" >> $DATAINITBAT;
echo "mysql -h127.0.0.1 -ubxn -p{bxnPassword} --default-character-set=utf8 < \"{BX_HOME}\\data\\init\\$CONTEXTPATH\\init_data.sql\"" >> $DATAINITBAT;
echo "mysql -h127.0.0.1 -ubxn -p{bxnPassword} --default-character-set=utf8 < \"{BX_HOME}\\data\\init\\$CONTEXTPATH\\init_bxn.sql\"" >> $DATAINITBAT;
echo "mysql -h127.0.0.1 -ubxn -p{bxnPassword} --default-character-set=utf8 < \"{BX_HOME}\\data\\init\\$CONTEXTPATH\\init_portal.sql\"" >> $DATAINITBAT;

# start bat
INSTALLSTARTBAT=$INSTALLBASE/bxn-install-extends-start.bat;
echo touch "$INSTALLSTARTBAT";
touch "$INSTALLSTARTBAT"
if [ -f "$INSTALLSTARTBAT" ];then
	echo touch "$INSTALLSTARTBAT" success!
else
	echo ERROR!! touch "$INSTALLSTARTBAT" failed!
	exit -1;
fi
echo "cd bx_app\\installer" >> $INSTALLSTARTBAT;
echo "java -classpath bxn-install-extends.jar; cn.boxiao.bxn.install.ApplicationLanuch" >> $INSTALLSTARTBAT;

echo zip begin
cd $INSTALLBASE
INSTALLZIP="bxn-install-extends-$CONTEXTPATH-$VERSION-t_x64_$CURRENTTIME.zip";
zip -r $INSTALLZIP *
if [ ! -d "/opt/build/$TODAY" ];then
	mkdir /opt/build/$TODAY
	echo mkdir /opt/build/$TODAY and chmod 777
	chmod 777 /opt/build/$TODAY;
else
	echo /opt/build/$TODAY is exits, chmod 777 only.
	chmod 777 /opt/build/$TODAY;
fi
mv $INSTALLZIP /opt/build/$TODAY/
echo package end
rm -rf $INSTALLBASE
echo "$(date +%Y%m%d%k%M%S)" package end.
